package com.ytlz.dmcs.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.ytlz.dmcs.model.system.dao.UserDao;
import com.ytlz.dmcs.model.system.entity.SysUser;


@Component
public class SysUserDetailsService implements UserDetailsService {

	@Autowired
	private UserDao userservice;
	
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		SysUser sysUser = userservice.findByUsername(username);
		if(sysUser ==null)
			throw new UsernameNotFoundException(String.format("No user found with username '%s'.", username));
		System.out.println(sysUser.getRoles().get(0).getName());
		User user = new User(sysUser.getUsername(),sysUser.getPassword(),AuthorityUtils.commaSeparatedStringToAuthorityList(sysUser.getRoles().get(0).getName()));		
		return user;
	}

}
